/**********************************************************************/
/*
   Author: Karan Makkar
   Created: Oct  2023

   Description: Images for
   Figure 2: CDF of Days between Winning and Receiving Cash, by Report
   Apply/Win
   Figure 3: PDF of Days between Winning and Receiving Cash, by Report
   Apply/Win

   Output:
   Figure2A, Figure2B, Figure2C
   Figure3A, Figure3B, Figure3C

*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

  clear all
  set more off
  set matsize 11000

* Log
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_win_getcash_diff_dist.txt", text replace
  graph set window fontface "Times New Roman"

* Image filetype
local ftype "pdf"
/*----------------------------------------------------*/
        /* Section: Prepare Batch Schedule Data */
/*----------------------------------------------------*/

import excel "$KP_deid_admin/Raw/00 Kartu Prakerja Batch Schedule.xlsx", sheet("Batch opening schedule") firstrow clear

replace batch = subinstr(batch, "*", "", .)
destring batch, replace

keep batch announcement

drop if mi(batch)

rename batch batch_treated_39
format announcement %tdMon_DD_CCYY

tempfile batch_dates
sa `batch_dates'

/*----------------------------------------------------*/
        /* Report Apply */
/*----------------------------------------------------*/

u anon_id4 batch_treated_39 date_incentive report_applied sak_round first_apply_batch using "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta", clear
append using "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive report_applied sak_round first_apply_batch)
append using "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive report_applied sak_round first_apply_batch)

*Apply before survey var
gen apply_before_survey = 0
replace apply_before_survey  = 1 if sak_round == 5 & first_apply_batch <=3
replace apply_before_survey  = 1 if sak_round == 6 & first_apply_batch <=11
replace apply_before_survey  = 1 if sak_round == 7 & first_apply_batch <=17
gegen apply_before_survey = max(apply_before_survey), by(anon_id4 sak_round) replace

bysort anon_id4: gegen date_incentive = max(date_incentive), replace

bysort anon_id4 sak_round: keep if _n ==1

keep if batch_treated_39 <= 22 & !mi(date_incentive) & apply_before_survey ==1

fmerge m:1 batch_treated_39 using `batch_dates', nogen assert(2 3) keep(3)

gen diff = date_incentive-announcement

/*********
PDFs
**********/
* PDFs by Report Applied

gen diff_l50 = diff <=50
summ diff_l50
local prop = r(mean)

* Diff <50
codensity gen diff if diff <=50, over(report_applied) 

codensity super, ///
 recast(area) ///
  opt1(lcolor(orange) color(orange%40)) opt2(lcolor(blue) color(blue%40)) ///
  xtitle("Days between winning and receiving the cash transfer", size(medlarge)) ///
  legend(order(1 "Report Didn't Apply" 2 "Report Applied") size(medlarge)) ///
  ylabel(0 "0" .0209 ".02" 0.0419 ".04" .0628 ".06" .0838 ".08", labsize(medlarge)) ///
  xla(, labsize(medlarge)) ytitle(, size(medlarge)) ///
  legend(ring(0) pos(2))
graph export $KP_output/figures/descriptive/`ftype'/Figure3A.`ftype', as(`ftype') replace

/*********
CDFs
**********/
  

summ diff if report_applied == 0 & diff <=50
local mean1 =r(mean)
local mean1: di %4.1f `mean1'
summ diff if report_applied == 1 & diff <=50
local mean2 =r(mean)
local mean2: di %4.1f `mean2'

* Diff < 50
distplot diff if diff <= 50, over(report_applied) ///
 c(J J) lc(red blue) ///
 legend(order(1 "Report Didn't Apply" 2 "Report Applied") size(medlarge)) /// 
 legend(ring(0) pos(4)) ///
 xtitle("Days Between Winning and Receiving Cash Transfer", size(medlarge)) ///
 xline(`mean1', lcolor(red) lpattern(dash)) xline(`mean2', lcolor(blue) lpattern(dash)) ///
 yla(0 "0" 0.209 "0.2" 0.419 "0.4" 0.628 "0.6" 0.838 "0.8" 1.048 "1", labsize(medlarge)) ///
 xla(, labsize(medlarge)) ytitle(, size(medlarge))
  
graph export $KP_output/figures/descriptive/`ftype'/Figure2A.`ftype', as(`ftype') replace

/*----------------------------------------------------*/
        /* Report Win */
/*----------------------------------------------------*/

/*****************
Susenas
******************/

u anon_id4 batch_treated_39 date_incentive report_selected sak_round using "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta", clear
append using "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive report_selected sak_round)
append using "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive report_selected sak_round)

gen cash_before_survey = 0
replace cash_before_survey  = 1 if sak_round == 5 & inrange(batch, 2, 3) & date_incentive <= date("2020/7/31", "YMD")
replace cash_before_survey  = 1 if sak_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/1/31", "YMD")
replace cash_before_survey  = 1 if sak_round == 7 & inrange(batch, 2, 17) & date_incentive <= date("2021/7/31", "YMD")
gegen cash_before_survey = max(cash_before_survey), by(anon_id4 sak_round) replace

bysort anon_id4: gegen date_incentive = max(date_incentive), replace

bysort anon_id4 sak_round: keep if _n ==1

keep if cash_before_survey ==1

keep if batch_treated_39 <= 22 & !mi(date_incentive)

fmerge m:1 batch_treated_39 using `batch_dates', nogen assert(2 3) keep(3)

gen diff = date_incentive-announcement

/*********
PDFs
**********/

gen diff_l50 = diff <=50
summ diff_l50
local prop = r(mean)

* Diff <50
codensity gen diff if diff <=50, over(report_selected) 

codensity super, ///
 recast(area) ///
  opt1(lcolor(orange) color(orange%40)) opt2(lcolor(blue) color(blue%40)) ///
  xtitle("Days between winning and receiving the cash transfer", size(medlarge)) ///
  ytitle(, size(medlarge)) ///
  legend(order(1 "Report Didn't Win" 2 "Report Won") size(medlarge)) ///
  ylabel(0 "0" .0209 ".02" 0.0419 ".04" .0628 ".06" .0838 ".08", labsize(medlarge)) ///
  xla(, labsize(medlarge)) ///
  legend(ring(0) pos(2))
graph export $KP_output/figures/descriptive/`ftype'/Figure3B.`ftype', as(`ftype') replace

/*********
CDFs
**********/
  

summ diff if report_selected == 0 & diff <=50
local mean1 =r(mean)
local mean1: di %4.1f `mean1'
summ diff if report_selected == 1 & diff <=50
local mean2 =r(mean)
local mean2: di %4.1f `mean2'

* Diff < 50
distplot diff if diff <= 50, over(report_selected) ///
 c(J J) lc(red blue) ///
 legend(order(1 "Report Didn't Win" 2 "Report Won") size(medlarge)) /// 
 legend(ring(0) pos(4)) ///
 xtitle("Days Between Winning and Receiving Cash Transfer", size(medlarge)) ///
 ytitle(, size(medlarge)) ///
  xline(`mean1', lcolor(red) lpattern(dash)) xline(`mean2', lcolor(blue) lpattern(dash)) ///
  yla(0 "0" 0.209 "0.2" 0.419 "0.4" 0.628 "0.6" 0.838 "0.8" 1.048 "1", labsize(medlarge)) ///
  xla(, labsize(medlarge))
  
graph export $KP_output/figures/descriptive/`ftype'/Figure2B.`ftype', as(`ftype') replace

/*****************
Susenas
******************/

u anon_id4 batch_treated_39 date_incentive get_pk sus_round using "$KP_deid_susenas/Clean/sus_sep20_deid_clean_merged.dta", clear
append using "$KP_deid_susenas/Clean/sus_mar21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive hh_pk_win sus_round)
append using "$KP_deid_susenas/Clean/sus_sep21_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive hh_pk_win sus_round)
append using "$KP_deid_susenas/Clean/sus_mar22_deid_clean_merged.dta", keep(anon_id4 batch_treated_39 date_incentive get_pk sus_round)

gen cash_before_survey = 0
replace cash_before_survey  = 1 if sus_round == 5 & inrange(batch, 2, 5) & date_incentive <= date("2020/8/30", "YMD")
replace cash_before_survey  = 1 if sus_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/2/28", "YMD")
replace cash_before_survey  = 1 if sus_round == 7 & inrange(batch, 2, 18) & date_incentive <= date("2021/8/30", "YMD")
replace cash_before_survey  = 1 if sus_round == 8 & inrange(batch, 2, 22) & date_incentive <= date("2022/2/28", "YMD")
gegen cash_before_survey = max(cash_before_survey), by(anon_id4 sus_round) replace

bysort anon_id4: gegen date_incentive = max(date_incentive), replace

bysort anon_id4 sus_round: keep if _n ==1

keep if cash_before_survey ==1

keep if batch_treated_39 <= 22 & !mi(date_incentive)

fmerge m:1 batch_treated_39 using `batch_dates', nogen assert(2 3) keep(3)

gen diff = date_incentive-announcement

cap drop report_selected
gen report_selected =.
replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)
replace report_selected = get_pk if inlist(sus_round, 5, 8)

/*********
PDFs
**********/
* PDFs by Report Win

gen diff_l50 = diff <=50
summ diff_l50
local prop = r(mean)

* Diff <50
codensity gen diff if diff <=50, over(report_selected) 

codensity super, ///
 recast(area) ///
  opt1(lcolor(orange) color(orange%40)) opt2(lcolor(blue) color(blue%40)) ///
  xtitle("Days Between Winning and Receiving Cash Transfer", size(medlarge)) ///
  ytitle(, size(medlarge)) ///
  legend(order(1 "Report Didn't Win" 2 "Report Won") size(medlarge)) ///
  ylabel(0 "0" .0209 ".02" 0.0419 ".04" .0628 ".06" .0838 ".08" , labsize(medlarge)) ///
  xla(, labsize(medlarge)) ///
  legend(ring(0) pos(2))
graph export $KP_output/figures/descriptive/`ftype'/Figure3C.`ftype', as(`ftype') replace

/*********
CDFs
**********/
  

summ diff if report_selected == 0 & diff <=50
local mean1 =r(mean)
local mean1: di %4.1f `mean1'
summ diff if report_selected == 1 & diff <=50
local mean2 =r(mean)
local mean2: di %4.1f `mean2'

* Diff < 50
distplot diff if diff <= 50, over(report_selected) ///
 c(J J) lc(red blue) ///
 legend(order(1 "Report Didn't Win" 2 "Report Won") size(medlarge)) /// 
 legend(ring(0) pos(4)) ///
 xtitle("Days Between Winning and Receiving Cash Transfer", size(medlarge)) ///
 ytitle(, size(medlarge)) ///
  xline(`mean1', lcolor(red) lpattern(dash)) xline(`mean2', lcolor(blue) lpattern(dash)) ///
  yla(0 "0" 0.209 "0.2" 0.419 "0.4" 0.628 "0.6" 0.838 "0.8" 1.048 "1", labsize(medlarge)) ///
  xla(, labsize(medlarge))
  
graph export $KP_output/figures/descriptive/`ftype'/Figure2C.`ftype', as(`ftype') replace
